package apps.softtech.fastmarket;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ListView;
import apps.softtech.Utility.ConnectionDetector;
import apps.softtech.Utility.RequestDataFromServer;
import apps.softtech.fastmarket.adapter.AdapterSearchMyProduct;
import apps.softtech.fastmarket.model.Product;

public class ActivityMyProduct extends Activity{
	ArrayList<Product> mProductsList = new ArrayList<Product>();
	AdapterSearchMyProduct mAdapterSearchMyProduct;
	DisplayImageOptions options;
	
	ListView lvProdutsList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.page_my_profile_my_product);
		/**
		 * init imageloader
		 */
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(ActivityMyProduct.this).build();
    	ImageLoader.getInstance().init(config);
    	/**
    	 * init display bitmap
    	 */
    	options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_launcher)
		.showImageForEmptyUri(R.drawable.ic_launcher)
		.showImageOnFail(R.drawable.ic_launcher)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.displayer(new RoundedBitmapDisplayer(0))
		.build();
		
    	init();
    	initFunction();
		
	}
	private void init() {
		lvProdutsList = (ListView)findViewById(R.id.lvProductsList);
	}
	private void initFunction() {
		ConnectionDetector cd = new ConnectionDetector(ActivityMyProduct.this);
		if(cd.isConnectingToInternet()){
			new AsyncTaskGetMyProduct().execute();
		}
		else{
			//load tu database and setdata
			setData();
		}
	}
	
	class AsyncTaskGetMyProduct extends AsyncTask<Void, Void, Void>{
		ProgressDialog pd;
		@Override
		protected void onPreExecute() {
			pd = new ProgressDialog(ActivityMyProduct.this);
			pd.setMessage("Loading....");
			pd.show();
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(Void... params) {
			try{
				JSONArray jsonData = new JSONArray();
				JSONObject jsonCategoryParentList = new JSONObject(new RequestDataFromServer()
				.requestMyProductd(2));
				int success = jsonCategoryParentList.getInt("success");
				if(success==1){
					jsonData = jsonCategoryParentList.getJSONArray("data");
					Log.v("", jsonData.toString());
					for(int i=0;i<jsonData.length();i++){
						JSONObject jsonList = new JSONObject(jsonData.getJSONObject(i).toString());
//						
						Product mpProduct = new Product();
//						mpProduct.setname;
//						mpProduct.setavartar;
						mProductsList.add(mpProduct);
					}
				}
				}catch(Exception e){}
				Log.v("", "");
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			setData();
			pd.dismiss();
			super.onPostExecute(result);
		}
		
		
	}
	private void setData() {
		mAdapterSearchMyProduct = new AdapterSearchMyProduct(ActivityMyProduct.this, mProductsList, options);
		lvProdutsList.setAdapter(mAdapterSearchMyProduct);
	}
}
